package com.keste.ojt1.view.task10.kvaka.bean;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

import java.util.StringTokenizer;

import javax.el.ExpressionFactory;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.view.rich.component.rich.input.RichInputFile;

import org.apache.myfaces.trinidad.model.UploadedFile;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

public class FileUpload {
    private RichInputFile uploadfile;

    public FileUpload() {
    }

    public void setUploadfile(RichInputFile uploadfile) {
        this.uploadfile = uploadfile;
    }

    public RichInputFile getUploadfile() {
        return uploadfile;
    }

    public void fileprocessor(ValueChangeEvent valueChangeEvent) {
    }
    private RichInputFile uploadFile;
    private BindingContainer bindings = null;

    /**
     * This method returns the instance of BindingContainer.
     * @return BindingContainer.
     */
    private BindingContainer getBindings() {

        if (this.bindings == null) {
            this.bindings = (BindingContainer)getDCBindingContainer();
        }
        return bindings;
    }

    /**
     * generic method to get handle to DCBindingContainer.
     * @return DCBindingContainer handle
     */
    public DCBindingContainer getDCBindingContainer() {

        FacesContext fctx = FacesContext.getCurrentInstance();
        ExpressionFactory expFactory =
            FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
        DCBindingContainer bc =

            (DCBindingContainer)expFactory.createValueExpression(fctx.getELContext(),
                                                                 "#{bindings}",
                                                                 DCBindingContainer.class).getValue(fctx.getELContext());

        return bc;
    }

    public void setUploadFile(RichInputFile uploadFile) {
        this.uploadFile = uploadFile;
    }

    public RichInputFile getUploadFile() {
        return uploadFile;
    }

    public void Fileprocessor(ValueChangeEvent valueChangeEvent) throws Exception {
        UploadedFile upFile = (UploadedFile)valueChangeEvent.getNewValue();

        List list = new ArrayList();

        try {
            BufferedReader reader =
                new BufferedReader(new InputStreamReader(upFile.getInputStream()));

            for (int i = 0; i < 7; i++) {
                String line = reader.readLine();
                StringTokenizer stok = new StringTokenizer(line, ",");
                List list1 = new ArrayList();
                while (stok.hasMoreTokens()) {

                    list1.add(stok.nextToken());
                }
                list.add(list1);
            }
            System.out.println("Token2: " + list);
            BindingContainer bindings = getBindings();
            OperationBinding op =
                bindings.getOperationBinding("setCSVDatatoVO");
            op.getParamsMap().put("list", list);
            if (op != null) {
                op.execute();
            }
        } catch (Exception e) {
            System.out.println("Trace: " + e);
        }
    }

    /*  public void validateRows(ActionEvent actionEvent) {

            BindingContainer bindings = getBindings();
                          OperationBinding op = bindings.getOperationBinding("setDateValidationVO");
                  op.execute();*/
}


